package com.admore.demo.nativiead;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;


import com.admore.demo.App;
import com.admore.demo.R;
import com.admore.demo.util.UIUtils;
import com.admore.sdk.ads.AdMoreNativeAd;
import com.admore.sdk.ads.NativeAd;
import com.admore.sdk.callback.AdMoreLoadListener;
import com.admore.sdk.callback.AdMoreMediaListener;
import com.admore.sdk.config.AdMoreConstants;
import com.admore.sdk.sdk.AdMoreSdk;
import com.zcy.base.ui.BaseActivity;

import java.util.HashMap;

public class FeedNativeActivity extends BaseActivity {

    private ViewGroup adMain;
    private AdMoreNativeAd adMoreNativeAd;

    @Override
    public int bindLayout() {
        return R.layout.activity_feed;
    }

    @Override
    public void initView() {
        adMain = findViewById(R.id.ad_main);
        ((TextView) findViewById(R.id.title)).setText("feed native");
        ((TextView) findViewById(R.id.title)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (adMoreNativeAd == null) {
                    loadAd();
                } else {
                    if (adMoreNativeAd.getNativeAds().size() > 0) {
                        showAd();
                    } else {
                        adMoreNativeAd.loadAd();
                    }
                }
            }
        });
    }

    @Override
    public void initData() {
        loadAd();
    }


    private void loadAd() {
        HashMap<String, Object> options = new HashMap<>();
        options.put(AdMoreConstants.AD_WIDTH, UIUtils.getScreenWidth(getApplicationContext()));
        options.put(AdMoreConstants.AD_HEIGHT, UIUtils.getScreenHeight(getApplicationContext()));
        adMoreNativeAd = AdMoreSdk.getAdMoreSdkManager().createNative(this, App.AD_MORE_NATIVE_ID, options);

        adMoreNativeAd.setLoadListener(new AdMoreLoadListener() {
            @Override
            public void onAdLoadSuccess(String msg) {
                Log.d(TAG, "onAdLoadSuccess");
                showAd();
            }

            @Override
            public void onAdLoadError(Throwable throwable) {
                Log.d(TAG, "onError " + throwable);
            }
        });

        adMoreNativeAd.loadAd();
    }

    private void showAd() {
        adMain.removeAllViews();
        if (adMoreNativeAd.getNativeAds() != null && adMoreNativeAd.getNativeAds().size() > 0) {
            final NativeAd nativeAd = adMoreNativeAd.getNativeAds().remove(0);
            nativeAd.setMediaListener(new AdMoreMediaListener() {
                @Override
                public void onAdClosed() {
                    Log.d(TAG, "onAdClosed");
                }
            });
            nativeAd.setAdMoreRenderListener(new com.admore.sdk.callback.AdMoreRenderListener() {
                @Override
                public void onRenderSuccess() {
                    Log.d(TAG, "onRenderSuccess");
                }

                @Override
                public void onError(Throwable throwable) {
                    Log.d(TAG, "onRenderError " + throwable);

                }
            });
            nativeAd.setMediaListener(new AdMoreMediaListener() {
                @Override
                public void onAdClosed() {
                    Log.d(TAG, "onAdClosed");
                }

                @Override
                public void onAdShow() {
                    Log.d(TAG, "onAdShow ecpm:" + nativeAd.getAdMoreInfo().getECPM() + " adName:" + nativeAd.getAdMoreInfo().getSdkName());
                }
            });
            nativeAd.render(this, adMain);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (adMoreNativeAd != null) {
            adMoreNativeAd.destroy();
        }
    }
}
